package org.jeecg.cn.edu.scau.vm.review.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.cn.edu.scau.vm.common.consts.ScheduleConst;
import org.jeecg.cn.edu.scau.vm.container.service.VmContainerBizService;
import org.jeecg.cn.edu.scau.vm.container.vo.VmContainerVo;
import org.jeecg.cn.edu.scau.vm.image.domain.VmImage;
import org.jeecg.cn.edu.scau.vm.image.service.VmImageService;
import org.jeecg.cn.edu.scau.vm.node.domain.VmNode;
import org.jeecg.cn.edu.scau.vm.node.service.NodeService;
import org.jeecg.cn.edu.scau.vm.review.entity.VmSchedule;
import org.jeecg.cn.edu.scau.vm.review.enums.VmScheduleEnum;
import org.jeecg.cn.edu.scau.vm.review.mapper.VmScheduleMapper;
import org.jeecg.cn.edu.scau.vm.review.service.impl.IVmScheduleService;
import org.jeecg.cn.edu.scau.vm.review.vo.VmScheduleVo;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service("vmScheduleService")
public class VmScheduleServiceImpl
        extends ServiceImpl<VmScheduleMapper, VmSchedule>
        implements IVmScheduleService {
    @Resource
    private VmScheduleMapper scheduleMapper;
    @Resource
    private VmContainerBizService vmContainerBizService;
    @Resource
    private VmImageService vmImageService;
    @Resource
    private NodeService nodeService;

    @Override
    public void schedulePass(String id, String reviewMsg) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        scheduleMapper.updateApproveStatusAndMsg(id, reviewMsg, loginUser.getUsername(), loginUser.getId(), VmScheduleEnum.PASS.getCode());
        // 此时需要创建容器,需要复原相关数据
        VmSchedule vmSchedule = scheduleMapper.selectById(id);
        // 1. 获取镜像信息
        VmImage vmImage = vmImageService.getByImageId(vmSchedule.getImageId());
        // 2. 补充用户信息
        loginUser = new LoginUser();
        loginUser.setId(vmSchedule.getUserId());
        loginUser.setUsername(vmSchedule.getUserName());
        loginUser.setOrgCode(vmSchedule.getSysOrgCode());
        // 3. 获取节点信息
        VmNode vmNode = nodeService.selectNode();
        // 4. 根据工单信息还原vmContainerVo
        VmContainerVo vmContainerVo = this.transferScheduleToContainerVo(vmSchedule);
        // 执行创建
        vmContainerBizService.createContainer(vmImage, loginUser, vmContainerVo, vmNode);
    }

    private VmContainerVo transferScheduleToContainerVo(VmSchedule schedule) {
        VmContainerVo vmContainerVo = new VmContainerVo();
        vmContainerVo.setFastCreate(true);
        vmContainerVo.setMachineTime(schedule.getMachineTime());
        vmContainerVo.setGpuCardNumber(schedule.getGpuNum());
        vmContainerVo.setName(schedule.getName());
        vmContainerVo.setDescription(schedule.getDescription());
        return vmContainerVo;
    }


    @Override
    public void scheduleReject(String id, String reviewMsg) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        scheduleMapper.updateApproveStatusAndMsg(id, reviewMsg, loginUser.getUsername(), loginUser.getId(), VmScheduleEnum.REJECT.getCode());
    }

    @Override
    public IPage<VmScheduleVo> pageDetail(Page<VmSchedule> page, Wrapper<VmSchedule> queryWrapper) {
        Page<VmSchedule> rawPage = this.page(page, queryWrapper);
        IPage<VmScheduleVo> newPage = new Page<>();
        // 1. 复制属性
        BeanUtils.copyProperties(rawPage, newPage, "records");
        List<VmScheduleVo> newRecord = rawPage.getRecords().stream().map((vmSchedule -> {
            VmScheduleVo vmScheduleVo = new VmScheduleVo();
            BeanUtils.copyProperties(vmSchedule, vmScheduleVo);
            vmScheduleVo.setApproveStatusName(ScheduleConst.getEnumByCode(vmSchedule.getApproveStatus()).getMessage());
            return vmScheduleVo;
        })).collect(Collectors.toList());
        newPage.setRecords(newRecord);
        return newPage;
    }
}
